//
//  HomeViewController.m
//  Weibo
//
//  Created by qiang on 4/21/16.
//  Copyright © 2016 QiangTech. All rights reserved.
//

#import "HomeViewController.h"

#import "VisitorView.h"
#import "UIBarButtonItem+Create.h"
#import "TitleButton.h"


#import "PopoverViewController.h"
#import "PopoverPresentationController.h"
#import "PopoverAnimator.h"


@interface HomeViewController ()

// 转场动画代理对象
@property (nonatomic,strong) PopoverAnimator *popoverAnimator;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(!self.userLogin)
    {
        [self.vistorView setupVisitorInfo:YES imageName:@"visitordiscover_feed_image_house" message:@"关注一些人，回这里看看有什么惊喜"];
    }
    
    // 设置导航浪上的按钮
    [self setupNavi];
    
    // 监听菜单打开关闭通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:kNontificationPopoverWillShow object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:kNontificationPopoverWillDismiss object:nil];
}

#pragma mark - 懒加载
- (PopoverAnimator *)popoverAnimator
{
    if(_popoverAnimator == nil)
    {
        _popoverAnimator = [[PopoverAnimator alloc] init];
        _popoverAnimator.presentFrame = CGRectMake(55, 56, 200, 350);
    }
    return _popoverAnimator;
}

- (void)change
{
    TitleButton *titleBtn = self.navigationItem.titleView;
    titleBtn.selected = !titleBtn.selected;
}

- (void)dealloc
{
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setupNavi
{
    // 1. 添加左右的按钮
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem createBarButtonItem:@"navigationbar_friendattention" target:self action:@selector(leftItemPressed:)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem createBarButtonItem:@"navigationbar_pop" target:self action:@selector(rightItemPressed:)];
    
    
    // 2. 创建标题按钮
    TitleButton *titleBtn = [[TitleButton alloc] init];
    [titleBtn setTitle:@"Qiang" forState:UIControlStateNormal];
    [titleBtn addTarget:self action:@selector(titleBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [titleBtn sizeToFit];
    self.navigationItem.titleView = titleBtn;
}

- (void)leftItemPressed:(id)sender
{
    NSLog(@"%s",__func__);
}

- (void)rightItemPressed:(id)sender
{
    NSLog(@"%s",__func__);
}

// 自定义转场动画见:
//http://www.cnblogs.com/zanglitao/p/4167909.html

- (void)titleBtnPressed:(UIButton *)sender
{
    NSLog(@"%s",__func__);

    // 弹出菜单
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"PopoverViewController" bundle:nil];
    PopoverViewController *popVC = sb.instantiateInitialViewController;
//    UIPopoverController *popVC = [sb instantiateViewControllerWithIdentifier:@"PopoverViewController"];// 通过StoryBoard ID
    
    // 采用自定义转场动画
    // 自定义转场动画
    popVC.modalPresentationStyle = UIModalPresentationCustom;
    popVC.transitioningDelegate = self.popoverAnimator; // 有谁来告诉他，谁来负责转场动画
    [self presentViewController:popVC animated:YES completion:nil];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 0;
}





@end
